package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.MessageSelectCo;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.service.MessageService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/message")
@Api(value = "消息接口")
public class MessageController {
	@Autowired
	private MessageService messageService;

	/**
	 * 删除消息
	 * 
	 * @param messageId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "removeMessage", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除消息")
	public Map<String, Object> removeMessage(@ApiParam(value = "消息ID", required = true) @RequestParam(value = "messageId", required = false) int messageId,
			HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			if (messageService.removeMessage(userId, messageId)) {
				return MapConvert.getMap(ResultType.SUCCESS);
			}
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	/**
	 * 清空私信
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/clearMessage", method = RequestMethod.DELETE)
	@ApiOperation(value = "清空私信",httpMethod="DELETE",notes="清空私信")
	public Map<String, Object> clearMessage(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			if (messageService.clearMessage(userId)) {
				return MapConvert.getMap(ResultType.SUCCESS);
			}
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	/**
	 * 发送消息
	 * 
	 * @param byUserId
	 * @param content
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addMessage", method = RequestMethod.POST)
	@ApiOperation(value = "发送消息")
	public Map<String, Object> addMessage(@ApiParam(value = "收信人ID", required = true) @RequestParam(value = "byUserId", required = false) String byUserId,
			@ApiParam(value = "发送内容", required = true) @RequestParam(value = "content", required = false) String content, HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			if (messageService.sendMessage(userId, byUserId, content)) {
				return MapConvert.getMap(ResultType.SUCCESS);
			}
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}

	/**
	 * 获取全部消息
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getMessageList", method = RequestMethod.GET)
	@ApiOperation(value = "获取全部消息")
	public Map<String, Object> getMessageList(@ModelAttribute @Valid MessageSelectCo messageSelectCo, HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			messageSelectCo.setByUserId(userId);
			messageSelectCo.setUserId(userId);
			return MapConvert.getMap(ResultType.SUCCESS, messageService.getMessageList(messageSelectCo));
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	
	
	@RequestMapping(value = "readMessage", method = RequestMethod.POST)
	@ApiOperation(value = "读取消息")
	public Map<String, Object> readMessage(
			@ApiParam(value = "消息ID", required = true) @RequestParam(value = "messageId", required = false) int messageId
			,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			if (messageService.readMessage(userId, messageId)) {
				return MapConvert.getMap(ResultType.SUCCESS);
			}
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
}
